package com.hspedu.homework;

/**
 * @author Charlie
 * @version 1.0
 * @date 1/11/2025 下午2:11
 */
public class EmpHashTableDemo {

    public static void main(String[] args) {

        HashTable hashTable = new HashTable(7);
        hashTable.add(new Emp("gening1", 1));
        hashTable.add(new Emp("gening2", 2));
        hashTable.add(new Emp("gening3", 3));
        hashTable.add(new Emp("gening4", 4));
        hashTable.add(new Emp("gening5", 555));
        hashTable.add(new Emp("gening4", 4666));
        hashTable.add(new Emp("gening4", 477));
        hashTable.add(new Emp("gening4", 4777));
        hashTable.add(new Emp("gening4", 44777));
        hashTable.add(new Emp("gening4", 447774));




        hashTable.list();

        hashTable.searchEmpById(44777);

    }

}


class HashTable {
    private int maxsize;
    private EmpLinkedList[] empLinkedListArray;


    public HashTable(int maxsize) {
        this.maxsize = maxsize;
        empLinkedListArray = new EmpLinkedList[maxsize];
        for (int i = 0; i < maxsize; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }

    public void add(Emp emp) {
        empLinkedListArray[empLinkedListArrayNo(emp.id)].add(emp);
    }

    public int empLinkedListArrayNo(int n) {
        return n % maxsize;
    }

    public void list() {
        for (int i = 0; i < maxsize; i++) {
            empLinkedListArray[i].list(i);
            System.out.println();
        }


    }

    public Emp searchEmpById(int n){
        Emp empById =empLinkedListArray[empLinkedListArrayNo(n)].findEmpById(n);

        if(empById!=null){
            System.out.printf("find emp  in List-%d id=%d  name=%s\t\n",empLinkedListArrayNo(n),n,empById.name);
        }else {
            System.out.println("not found such emp");
        }

        return empById;



    }


}


class Emp {
    String name;
    Integer id;
    Emp next;

    public Emp(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}


class EmpLinkedList {


    private Emp head;


    //链表的add方法
    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp currentEmp = head;
        while (currentEmp.next != null) {
            currentEmp = currentEmp.next;
        }
        currentEmp.next = emp;


    }


    public void list(int n) {
        if (head == null) {
            System.out.println("the list" + (++n) + "is empty");
            return;
        }
        Emp currentEmp = head;
        System.out.println("the list" + (++n) + "is =======>");
        while (currentEmp != null) {


            System.out.printf("id=>%d,name=%s\t", currentEmp.id, currentEmp.name);
            currentEmp = currentEmp.next;
        }

    }

    public Emp findEmpById(int id) {


        Emp currentEmp = head;
        while (currentEmp!=null) {
            if (currentEmp.id == id) {
                return currentEmp;
            }
            currentEmp = currentEmp.next;
            }


        return  null;


    }

}



